use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder};
use env_logger;
use log::{info, error, debug};
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Debug, Serialize)]
struct GtpString {
    key: String,
    value: String,
}

#[post("/gtp")]
async fn gtp(req: web::Json<GtpString>) -> impl Responder {
    debug!("gtp, request_body:{:?}", req);
    let gtp = req.into_inner();
    HttpResponse::Ok().body(serde_json::to_string(&gtp).unwrap())
}



#[get("/")]
async fn hello() -> impl Responder {
    HttpResponse::Ok().body("Hello world!")
}

#[post("/echo")]
async fn echo(req_body: String) -> impl Responder {
    debug!("echo, request_body:{}", &req_body);
    HttpResponse::Ok().body(req_body)
}

async fn manual_hello() -> impl Responder {
    HttpResponse::Ok().body("Hey there!")
}



#[actix_web::main]
async fn main() -> std::io::Result<()> {
    std::env::set_var("RUST_LOG", "debug");
    env_logger::init();
    debug!("Staring server");
    info!("Staring server");
    let obj = GtpString{key: "1".to_owned(), value:"2".to_owned()};

    error!("Staring server, {:?}", serde_json::to_string(&obj));
    

    HttpServer::new(|| {
        App::new()
            .service(hello)
            .service(echo)
            .service(gtp)
            .route("/hey", web::get().to(manual_hello))
    })
    .bind(("127.0.0.1", 8080))?
    .run()
    .await
}
